package com.chwn.curiousdiary.ui.other;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.api.ISystemApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.utils.CustomToast;
import com.chwn.harulib.base.BaseActivity;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.mvp.rx.RxSchedulers;
import com.chwn.harulib.util.ToastUtil;
import com.gyf.barlibrary.ImmersionBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;

/**
 * Created by Myshikin on 2017/3/27.
 */

public class FeedBackActivity extends BaseActivity {

    @BindView(R.id.edt_content)
    EditText  mContentET;
    @BindView(R.id.iv_feedback)
    ImageView mFeedbackIV;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        setTitleText("意见反馈");
        initnavigationBarColor(-1);
        //        mFeedbackIV.setAlpha(0.6f);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mContentET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(charSequence.toString()) && charSequence.toString().length() > 5) {
                    //                    mFeedbackIV.setEnabled(true);
                    //                    mFeedbackIV.setAlpha(1.0f);
                } else {
                    //                    mFeedbackIV.setEnabled(false);
                    //                    mFeedbackIV.setAlpha(0.6f);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.ib_back, R.id.iv_feedback})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.iv_feedback:
                checkInput();
                break;
        }
    }

    private void checkInput() {
        String feedBackContent = mContentET.getText().toString().trim();
        if (TextUtils.isEmpty(feedBackContent)) {
            ToastUtil.shortShowToast("请输入反馈内容");
            return;
        }

        feedBack(feedBackContent);
    }

    private void feedBack(String feedBackContent) {
        User user = DiaryApplication.mInstance.getmUser();
        String userToken = "";
        if (user != null) {
            userToken = user.getUserToken();
        }
        NetWork.getInstance()
                .getApiService(ISystemApi.class)
                .userFeedBack(feedBackContent, Constant.OSTYPE, 1, userToken)
                .compose(RxSchedulers.<HttpResult<Object>>io_main())
                .subscribe(new Observer<HttpResult<Object>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        errorAction("");
                    }

                    @Override
                    public void onNext(HttpResult<Object> objectHttpResult) {
                        if (objectHttpResult.getResult() == Constant.RESULT_ERROR) {
                            errorAction(objectHttpResult.getErrorMessage());
                            return;
                        }
                        CustomToast.showShortToast("提交成功 ，感谢您宝贵的意见");
                        finish();
                    }
                });
    }

    private void errorAction(String errorMsg) {
        String text;
        if (TextUtils.isEmpty(errorMsg)) {
            text = "请检查网络链接";
        } else {
            text = errorMsg;
        }

        CustomToast.showShortToast(text);
    }


   /* @Override
    protected boolean isDisableDrag() {
        return false;
    }*/

    /**
     * 上次点击退出时间
     */
    private long clickTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            backAction();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void backAction() {
        if (!TextUtils.isEmpty(mContentET.getText().toString().trim())) {
            if ((System.currentTimeMillis() - clickTime) > 2000) {
                Toast.makeText(getApplicationContext(), "还未提交 ，再次点击退出",
                        Toast.LENGTH_SHORT).show();
                clickTime = System.currentTimeMillis();
            } else {
                finish();
            }
        } else {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
